package com.fruitshop.manager.base;

import com.fruitshop.model.Command;
import com.fruitshop.util.PrintUtils;
import com.fruitshop.util.UserInfoContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;

// 抽象类，管理所有子类
public abstract class BaseManager {

    // 退出程序
    public static final String EXIT = "exit";

    // 返回上一级
    public static final String BACK = "0";


//    public static final String LOGIN_OUT = "logout";

    // 登录成功
    public static final String LOGIN_SUCCESS = "LOGIN-SUCCESS";

    public static final String LOGOUT_SUCCESS = "logout";
    // 正常退出
    public static final String NORMAL_EXIT = "NORMAL_EXIT";

    // 标题
    private String title;

    // 所有人都可以查看的命令列表
    private List<Command> commandList;

    // 不需要登录的命令列表
    private List<Command> noNeedLoginCommandList;




    public void setTitle(String title) {
        this.title = title;
    }

    // 是否是根节点
    private boolean isRoot=false;

    public BaseManager(String title, List<Command> commandList, boolean isRoot) {

        this.isRoot=isRoot;

        for (Command command : commandList) {
            if (command.getId().equals(BACK) || command.getId() .equals(EXIT) || command.getId().equals(LOGOUT_SUCCESS)) {
                throw new RuntimeException("Command id can not be " + BACK + " or " + EXIT+" or " + LOGOUT_SUCCESS);
            }
        }
        commandList.add(0, new Command(BACK, "返回", false));
        commandList.add(new Command(LOGOUT_SUCCESS, "退出登录", true));
        commandList.add(new Command(EXIT, "退出程序", false));

        this.title = title;
        this.commandList = commandList;

        noNeedLoginCommandList = commandList.stream().filter(command -> !command.isNeedLogin()).collect(Collectors.toList());

    }

    public BaseManager(String title, List<Command> commandList) {
       this(title,commandList,false);
    }

    // 抽象方法，每个子类都要实现
    public final String run() {



        ;


        Scanner scanner = new Scanner(System.in);

        while (true) {

            printTitle();

            List<Command> showCommandList = filterShowCommandList();

            printCommandList(showCommandList);

            PrintUtils.newLine();
            printTitleExtra();

            String command = null;

            //拿到所有需要登陆的命令ID
            Set<String> ids = showCommandList.stream().map(Command::getId).collect(Collectors.toSet());

//            if(UserInfoContext.isAdmin()){
//                ids = adminCommandList.stream().map(Command::getId).collect(Collectors.toSet());
//
//            }


             command = scanner.next();
            if (!ids.contains(command)) {
                PrintUtils.printError("输入错误，请重新输入");
                continue;
            }




            if (command.equals(EXIT) ) {
                exit(0);
            }
            if (command.equals(BACK)) {
                return NORMAL_EXIT;
            }

            if(command.equals(LOGOUT_SUCCESS)){

                UserInfoContext.remove();

                if(isRoot){
                    continue;
                }else{
                    return LOGOUT_SUCCESS;
                }
            }


            String command1 = __processCommand(command);

            if (command1.equals(LOGIN_SUCCESS)&&!isRoot) {
                return LOGIN_SUCCESS;
            }

//            if (exit == BACK) {
//                break;
//            }
        }
    }

    private List<Command> filterShowCommandList() {
        //拿到所有不需要登陆的命令ID

        List<Command> showCommandList = new ArrayList<>();

        if (UserInfoContext.isLogin()) {
            if(UserInfoContext.isAdmin()){
                List<Command> list = commandList.stream().filter(command -> command.getScope().equals(Command.SCOPE_ADMIN) || command.getScope().equals(Command.SCOPE_ALL)).collect(Collectors.toList());
                showCommandList.addAll(list);
            }else{
                List<Command> list = commandList.stream().filter(command -> command.getScope().equals(Command.SCOPE_USER) || command.getScope().equals(Command.SCOPE_ALL)).collect(Collectors.toList());
                showCommandList.addAll(list);
            }
        }else{
            showCommandList.addAll(noNeedLoginCommandList);
        }
        return filterShowCommandList(showCommandList);
    }

    protected List<Command> filterShowCommandList(List<Command> showCommandList) {
        return showCommandList;
    }

    // 打印 标题
    protected void printTitle() {
        PrintUtils.newLine();
        PrintUtils.println("***********%s***********", title);
        if(UserInfoContext.isLogin()) {
            if(UserInfoContext.isAdmin()){
                PrintUtils.printHighLight(String.format("当前登陆管理员：%s", UserInfoContext.getAdmin().getName()));
            }else{
                PrintUtils.printHighLight(String.format("当前登陆用户：%s", UserInfoContext.getUser().getName()));
            }
        }
        PrintUtils.newLine();
    }

    protected void printTitleExtra() {
    }

    // 打印命令列表
    protected void printCommandList(List<Command> showCommandList) {

        showCommandList.forEach(command -> {
            printCommand(command);
        });
    }

    protected void printCommand(Command command) {
        PrintUtils.printCommand(command);
    }
    // 返回上一级
    protected int back() {
        return 0;
    }

    private String __processCommand(String command) {



        return processCommand(command);
    }

    // 具体处理命令 抽象方法，每个子类都要实现
    protected abstract String processCommand(String command);


    // 退出程序
    private void exit(int status) {
        System.exit(status);
    }
}
